package com.zhihuitong.meeting.utils;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.mob.MobSDK;
import com.mob.OperationCallback;
import com.mob.PrivacyPolicy;
import com.zhihuitong.meeting.R;
import com.zhihuitong.meeting.dao.LocalMeetingBean;
import com.zhihuitong.meeting.global.MyApplication;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

import cn.sharesdk.onekeyshare.OnekeyShare;

/**
 * 作者： guoyuan
 * 时间 2020/2/26.
 * 文件描述：
 * 版本号：V 1.0
 **/
public class ShareUtils {
    public static void showShare(final Context context, LocalMeetingBean entity) throws UnsupportedEncodingException {
        submitPrivacyGrantResult(true);
        LocalMeetingBean meetingBean = new LocalMeetingBean();
        meetingBean.setInviteName(MyApplication.getLocalUserName());
        meetingBean.setStartTime(URLDecoder.decode(entity.getStartTime(), "UTF-8"));
        meetingBean.setEndTime(URLDecoder.decode(entity.getEndTime(), "UTF-8"));
        meetingBean.setGeneralPwd(entity.getGeneralPwd());
        meetingBean.setCreateUserId(entity.getCreateUserId());
        meetingBean.setChairPwd(entity.getChairPwd());
        meetingBean.setSubject(entity.getSubject());
        meetingBean.setConferenceID(entity.getConferenceID());
        meetingBean.setMediaTypes("");
        meetingBean.setLength(entity.getLength());
        meetingBean.setConferenceState(entity.getConferenceState());
        meetingBean.setCreateTime(entity.getCreateTime());
        meetingBean.setAccessNumber(entity.getAccessNumber());
        meetingBean.setUserId(entity.getUserId());
        meetingBean.setScheduserName(entity.getScheduserName());

        OnekeyShare oks = new OnekeyShare(); //关闭sso授权 oks.disableSSOWhenAuthorize();
        oks.disableSSOWhenAuthorize();
        oks.setTitle("邀请参会");
        // titleUrl QQ和QQ空间跳转链接
        oks.setTitleUrl("https://www.hnzhihuitong.com/meet/join.html");
        // text是分享文本，所有平台都需要这个字段
        oks.setText(meetingBean.getInviteName() + "邀请您参加他的视频会议,赶紧点击链接进入吧!");
        // url在微信、微博，Facebook等平台中使用
        oks.setImageData(BitmapFactory.decodeResource(context.getResources(), R.drawable.app_logo));
        String params = Base64Util.getBase64(JsonUtil.toJson(meetingBean));
        // String url = "https://www.hnzhihuitong.com/meet2/join.html?params=" + params;
        // String url = "https://www.hnzhihuitong.com/meet2/join.html";
        String url = "https://www.hnzhihuitong.com/meet2/join.html?"
                + "&chairPwd=" + URLEncoder.encode(meetingBean.getChairPwd(), "UTF-8")
                + "&conferenceID=" + URLEncoder.encode(meetingBean.getConferenceID(), "UTF-8")
                + "&createTime=" + meetingBean.getCreateTime()
                + "&endTime=" + URLEncoder.encode(meetingBean.getEndTime(), "UTF-8")
                + "&generalPwd=" + URLEncoder.encode(meetingBean.getGeneralPwd(), "UTF-8")
                + "&inviteName=" + URLEncoder.encode(meetingBean.getInviteName(), "UTF-8")
                + "&length=" + meetingBean.getLength()
                + "&scheduserName=" + URLEncoder.encode(meetingBean.getScheduserName(), "UTF-8")
                + "&startTime=" + URLEncoder.encode(meetingBean.getStartTime(), "UTF-8")
                + "&subject=" + URLEncoder.encode(meetingBean.getSubject(), "UTF-8")
                + "&userId=" + URLEncoder.encode(meetingBean.getUserId(), "UTF-8");
        oks.setUrl(url);
        // 启动分享GUI
        oks.show(context);
    }


    private static void submitPrivacyGrantResult(boolean granted) {
        // 同步方法查询隐私,locale可以为null或不设置，默认使用当前系统语言
        MobSDK.getPrivacyPolicyAsync(MobSDK.POLICY_TYPE_TXT, new PrivacyPolicy.OnPolicyListener() {
            @Override
            public void onComplete(PrivacyPolicy data) {
                if (data != null) {
                    // 富文本内容
                    String text = data.getContent();
                }
            }

            @Override
            public void onFailure(Throwable t) {
                // 请求失败
            }
        });
        MobSDK.submitPolicyGrantResult(granted, new OperationCallback<Void>() {
            @Override
            public void onComplete(Void data) {
                Log.d("ShareSDK", "隐私协议授权结果提交：成功");
            }

            @Override
            public void onFailure(Throwable t) {
                Log.d("ShareSDK", "隐私协议授权结果提交：失败");
            }
        });
    }
}
